function [L]=likelihood(y,v,R)

%%%Extract parameter vector %%%;
alpha=y(1); 
alpha_2=y(2);
lambda_4=y(3); 
lambda_5=y(4); 
kappa =y(5);   
ln_sigma_v =y(6); 

%%%Define data elements%%%;
C_1=R(:,1); 
C_2=R(:,2); 
C_3=R(:,3); 
C_4=R(:,4);
C_5=R(:,5);
x_1=R(:,6);  
d_1=R(:,7); 
N=length(C_1); 

    c_1 = kappa*d_1 + alpha; %distance cost and type constant across time periods
    a = c_1;
    
%%Period 5 utilities;
    u_5 = a  + alpha_2 + v + lambda_5*(x_1==3);
    
%%Period 5 choice probabilities for each value of state;
    p_1_5=1./(1 + exp( - (u_5)));
    p_2_5=1-p_1_5;

%%Expectation of period 5 value function;
    E_V5 = log(1+exp(u_5)); 
    
%%Period 4 utilities;
    u_4 = a + alpha_2*4 + v  + lambda_4*(x_1==2) + E_V5;
    
%%Period 4 choice probabilities for each value of state;
    p_1_4=1./(1 + exp(-(u_4))); 
    p_2_4=1-p_1_4;  

%%Expectation of period 4 value function;
    E_V4 = log(1+exp(u_4)); 
     
%%Period 3 utilities;
    u_3 = a + alpha_2*3 + v + E_V4;
    
%%Period 3 choice probabilities for each value of state;
    p_1_3=1./(1 + exp(-(u_3)));
    p_2_3=1-p_1_3;

%%Expectation of period 3 value function;
    E_V3 = log(1+exp(u_3)); 
    
%%Period 2 utilities;
    u_2 = a + alpha_2 + v + E_V3;
    
%%Period 2 choice probabilities for each value of state;
    p_1_2=1./(1 + exp(-(u_2)));
    p_2_2=1-p_1_2;

%%Expectation of period 2 value function;
    E_V2 = log(1+exp(u_2));
    
%%Period 1 utilities;
    u_1 = a + v + E_V2; 
     
%%Period 1 choice probabilities for each value of state;
    p_1_1=1./(1 + exp(-(u_1)));
    p_2_1=1-p_1_1;    
      
%%%Likelihoods of 1st, 2nd, 3rd, 4th, 5th period choices;
%%First period;
P_1=p_1_1.*(C_1==1)+p_2_1.*(C_1==0);
%%Second period;
P_2=p_1_2.*(C_2==1& C_1==1)+p_2_2.*(C_2==0 & C_1==1);     
%%Third period;   
P_3=p_1_3.*(C_3==1& C_2==1 & C_1==1)+p_2_3.*(C_3==0 & C_2==1 & C_1==1);     
%%Fourth period;
P_4=p_1_4.*(C_4==1 & C_3==1 & C_2==1 & C_1==1)+p_2_4.*(C_4==0 & C_3==1 & C_2==1 & C_1==1);
%%Fifth period; 
P_5=p_1_5.*(C_5==1& C_4==1 & C_3==1 & C_2==1 & C_1==1)+p_2_5.*(C_5==0 & C_4==1 & C_3==1 & C_2==1 & C_1==1);   


%%%Final sample log likelihood; 
P1 = P_1.*(P_1~=0) + (P_1==0);
P2 = P_2.*(P_2~=0) + (P_2==0);
P3 = P_3.*(P_3~=0) + (P_3==0);
P4 = P_4.*(P_4~=0) + (P_4==0);
P5 = P_5.*(P_5~=0) + (P_5==0);

L = P1.*P2.*P3.*P4.*P5; 


